/**
 * 
 */
package smallChanges.statements;

import net.sourceforge.pmd.ast.AccessNode;

/**
 * Typesafe enum for representing the visibility modifiers of a field, method or class.
 * 
 * @author jspacco
 *
 */
public class SCVisibility
{
	public static final SCVisibility PUBLIC=new SCVisibility("public");
	public static final SCVisibility PRIVATE=new SCVisibility("private");
	public static final SCVisibility PROTECTED=new SCVisibility("protected");
	public static final SCVisibility PACKAGE_PRIVATE=new SCVisibility("");
	
	private String visibility;
	
	private SCVisibility(String visibility) {
		this.visibility=visibility;
	}
	
	public boolean requiresToken() {
	    return visibility.equals("public") || visibility.equals("private") ||
	        visibility.equals("protected");
	}
	
	public String toString() {
		return visibility;
	}
	
	public static SCVisibility createSCVisibility(String vis)
	{
	    if (vis.equals("packageprivate") || vis.equals("")) {
	        return SCVisibility.PACKAGE_PRIVATE;
	    }
	    if (vis.equals("public")) {
	        return SCVisibility.PUBLIC;
	    }
	    if (vis.equals("protected")) {
	        return SCVisibility.PROTECTED;
	    }
	    if (vis.equals("private")) {
	        return SCVisibility.PRIVATE;
	    }
	    throw new IllegalStateException("visibility must be one of the 4 possibilities");
	}
	
	public static SCVisibility createSCVisibility(AccessNode node)
	{
		if (node.isPackagePrivate()) {
			return SCVisibility.PACKAGE_PRIVATE;
		}
		if (node.isPrivate()) {
			return SCVisibility.PRIVATE;
		}
		if (node.isPublic()) {
			return SCVisibility.PUBLIC;
		}
		if (node.isProtected()) {
			return SCVisibility.PROTECTED;
		}
		throw new IllegalStateException("ASTMethodDeclaration node is neither " +
				"package-private, private, public, nor protected!  " +
				"How the hell did that happen?");
	}
	
	public boolean isPublic() {
		return this==PUBLIC;
	}
	public boolean isPrivate() {
		return this==PRIVATE;
	}
	public boolean isPackagePrivate() {
		return this==PACKAGE_PRIVATE;
	}
	public boolean isProtected() {
		return this==PROTECTED;
	}
	
}
